<template>
  <div id="home">
    <nav-bar class="home-nav">
       <div slot="center">
          购物街
     </div>
    </nav-bar>
<!--    <tab-control :titles="['流行','新款','精选']"  @tabclick="tabclick" ref="tabControl" class="tab-control"></tab-control>-->

    <!--    @pullingUp="loadMore"-->

<scroll class="content" ref="scroll" :probe-type="3" @scroll="contentScroll" :pull-up-load="true" @pullingUp="loadMore">
  <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"></home-swiper>
  <recommend-view :recommends="recommends"></recommend-view>
  <feature-view></feature-view>
<!--  class="tab-control"-->
  <tab-control :titles="['流行','新款','精选']"  @tabclick="tabclick" ref="tabControl" ></tab-control>
  <goods-list :goods="showGoods"></goods-list>
</scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>


  </div>
</template>

<script>
  import NavBar from "../../components/common/navbar/NavBar";
  import Scroll from "../../components/common/scroll/Scroll";

  import {debounce} from 'common/utils';

  import HomeSwiper from "./childComps/HomeSwiper";
  import RecommendView from "./childComps/RecommendView";
  import FeatureView from "./childComps/FeatureView";
  import TabControl from "../../components/content/tabControl/TabControl";
  import GoodsList from "../../components/content/goods/GoodsList";
  import BackTop from "../../components/content/backTop/BackTop";


  import { getHomeMultidata,getHomeData } from "../../network/home";

  export default {
    name: "Home",
    components:{
      NavBar,
      Scroll,
      HomeSwiper,
      RecommendView,
      FeatureView,
      TabControl,
      GoodsList,
      BackTop

      },
    data() {
          return{
            banners:[],
            recommends:[],
            goods:{
              'pop':{
                page:0,
                list:[]
              },
              'new':{
                page:0,
                list:[]
              },
              'sell':{
                page:0,
                list:[]
              }
            },
            currentType:'pop',
            isShowBackTop:false,
            tabOffsetTop:0,
            isTabFixed:false,
            saveY:0
          }
    },
    created() {
          this.getHomeMultidata()
          //2请求商品数据
           this.getHomeData('pop')
           this.getHomeData('new')
           this.getHomeData('sell')
    },
    mounted() {

      //图片记载完成的事件监听
      const refresh = debounce(this.$refs.scroll.refresh,200)
      //监听item的图片记载完成
      this.$bus.$on('itemIamgeLoad',()=>{
        refresh()
      })

    },
    methods:{
      /**
       * 事件监听
       * */
      tabclick(index){
        switch (index) {
          case 0:
            this.currentType='pop'
            break
          case 1:
            this.currentType='new'
            break
          case 2:
            this.currentType='sell'
            break
          default:
        }
      },
      backClick(){
        this.$refs.scroll.scrollTo(0,0,500)
      },
      contentScroll(position){
        //判断是否显示
        this.isShowBackTop =  (-position.y) < 1000 ? false:true
        //决定是否吸顶
        this.isTabFixed = (-position.y)>this.tabOffsetTop
      },
      loadMore(){
       this.getHomeData(this.currentType)
        this.$refs.scroll.refresh()
      },
      swiperImageLoad(){
        //所有组件有el属性,用户获取组件元素
        this.tabOffsetTop = this.$refs.tabControl.$el.offsetTop
      },
      /**
       * 网络请求
       */
      //1请求多个数据
      getHomeMultidata(){
        getHomeMultidata().then(res=>{
          console.log(res);
          this.banners = res.data.banner.list;
          this.recommends=res.data.recommend.list;
          console.log(this.banners);
          console.log(this.recommends)
        })
      },
      getHomeData(type){
        const page = this.goods[type].page +1
        //2请求商品数据
        getHomeData(type,page).then(res=>{
          console.log(res);
          this.goods[type].list.push(...res.data.list)
            this.goods[type].page += 1
            //完成加载更多
          this.$refs.scroll.finishPullUp()
        })
      }
    },
    computed:{
      showGoods(){
        return this.goods[this.currentType].list
      }
    },
    activated() {
      this.$refs.scroll.scrollTo(0,this.saveY,0)
      this.$refs.scroll.refresh
    },
    deactivated() {
      this.saveY = this.$refs.scroll.getScrollY
    }
  }
</script>

<style scoped>
  #home{
    padding-top: 44px;
    height: 100vh;
    position: relative;
  }
  .home-nav{
    background: var(--color-tint);
    color: #ffffff;
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    z-index: 9;
  }
  /*.tab-control{*/
  /*  position: sticky;*/
  /*  top: 44px;*/
  /*  z-index: 9;*/
  /*}*/
  .content{
      /*height: 300px;*/
      overflow: hidden;
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }
  /*.content{*/
  /*  height: calc(100% - 93px);*/
  /*  overflow: hidden;*/
  /*  margin-top: 44px;*/
  /*}*/


</style>
